<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class Akunrincian_model extends Model {

    protected $_table = 'AkunRincian';
    protected $_prefix = 'akunRincian';
    
    public function __construct() {
        // Call the Model constructor
        parent::__construct ();
    }

    public function add($data) {
        if (! $data || ! is_array ( $data ))
            return false;
        return $this->db->insert ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    }
    public function update($id, $data) {
        if (! $id || ! $this->get ( $id ) || ! $data)
            return array ();

        $this->db->where ( 'akunId', $id );
        $this->db->limit ( 1 );
        return $this->db->update ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    }
	public function delete($id) {
        if (! $id || ! $this->get ( $id ))
            return array ();
        $this->db->where ( 'akunId', $id );
        $this->db->limit ( 1 );
        return $this->db->delete ( $this->_table ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
    public function get($id) {
        if (! $id) 
            return array ();
        
        $this->db->select ( 'AkunRincian.*' );
        $this->db->select ( 'Akun.akunTipeId, Akun.akunKode, Akun.akunNama' );
        $this->db->select ( 'AkunTipe.akunTipe, AkunTipe.akunTipeNama' );
        
    	$this->db->from ( $this->_table );
        $this->db->join ( 'Akun', 'AkunRincian.akunId = Akun.akunId' );
        $this->db->join ( 'AkunTipe', 'Akun.akunTipeId = AkunTipe.akunTipeId' );
        
        $this->db->where ( 'AkunRincian.akunId', $id);
        $this->db->limit ( 1 );	
        $query = $this->db->get ();
        return $query->row_array ();
    }
    
    //Get By Year & Date
	public function getDetail($where=array()) {	
        $this->db->select ( 'AkunRincian.*' );
    	$this->db->from ( $this->_table );
    	
        if(isset($where['akunId']))
        	$this->db->where('akunId', $where['akunId']);
        if(isset($where['akunPeriodeTahun']))
        	$this->db->where('AkunRincian.akunPeriodeTahun', $where['akunPeriodeTahun']);	
        if(isset($where['akunPeriodeBulan']))
        	$this->db->where('AkunRincian.akunPeriodeBulan', $where['akunPeriodeBulan']);	
        	
        $this->db->order_by ( 'akunPeriodeTahun', 'desc' );	
        $this->db->order_by ( 'akunPeriodeBulan', 'desc');	
        $this->db->limit ( 1 );	
        $query = $this->db->get ();
        return $query->row_array ();
    }
    
    public function getList($limit = 100, $offset = 0, $where=array()) { 
        $this->db->select ( 'AkunRincian.*' );
        $this->db->select ( 'Akun.akunTipeId, Akun.akunKode, Akun.akunNama' );
        $this->db->select ( 'AkunTipe.akunTipe, AkunTipe.akunTipeNama' );
    	$this->db->from ( $this->_table );
        $this->db->join ( 'Akun', 'AkunRincian.akunId = Akun.akunId' );
        $this->db->join ( 'AkunTipe', 'Akun.akunTipeId = AkunTipe.akunTipeId' );
        
        if(isset($where['akunTipeId']))
        	$this->db->where('Akun.akunTipeId', $where['akunTipeId']);
		if(isset($where['akunPeriodeTahun']))
        	$this->db->where('AkunRincian.akunPeriodeTahun', $where['akunPeriodeTahun']);	
        if(isset($where['akunPeriodeBulan']))
        	$this->db->where('AkunRincian.akunPeriodeBulan', $where['akunPeriodeBulan']);	
        	
        $this->db->order_by ( 'Akun.akunTipeId', 'asc' );
        $this->db->order_by ( 'LENGTH(Akun.akunKode)', 'asc' );
        $this->db->order_by ( 'Akun.akunKode', 'asc' );
        
        $this->db->limit ( $limit, $offset );
        $query = $this->db->get ();
        return $query->result_array ();
    }
    public function getCount($where=array()) {
        $this->db->select ( 'count(AkunRincian.akunId) as count' );
        $this->db->from ( $this->_table );
        $this->db->join ( 'Akun', 'AkunRincian.akunId = Akun.akunId' );
        
        if(isset($where['akunTipeId']))
        	$this->db->where('Akun.akunTipeId', $where['akunTipeId']);
        if(isset($where['akunPeriodeTahun']))
        	$this->db->where('AkunRincian.akunPeriodeTahun', $where['akunPeriodeTahun']);	
        if(isset($where['akunPeriodeBulan']))
        	$this->db->where('AkunRincian.akunPeriodeBulan', $where['akunPeriodeBulan']);	
        	
        $query = $this->db->get ();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }
    
    //Akun History
	public function insertHistory($id) {
        if ( !$id )
            return false;

        return $this->db->query ( "INSERT INTO AkunHistory (akunId, historySaldo, historyPeriodeTahun,  historyPeriodeBulan) 
        							( SELECT akunId, akunSaldo,  akunPeriodeTahun,  akunPeriodeBulan FROM AkunRincian 
        							WHERE akunId = '".$id."')" ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
}